package com.easyrule;

import lombok.Data;
import lombok.Setter;
import org.easyrules.annotation.Action;
import org.easyrules.annotation.Condition;
import org.easyrules.annotation.Priority;
import org.easyrules.annotation.Rule;
import org.easyrules.api.RulesEngine;
import org.easyrules.core.CompositeRule;
import org.easyrules.core.RulesEngineBuilder;
import org.omg.CORBA.INTERNAL;

import java.lang.reflect.Field;
import java.nio.channels.Channel;

/**
 * <p>
 * 规则测试
 * 输入 输入1--100的数字
 * 7的倍数 输出 我是一只鸡
 * 2的位数 输出 我是一只鸭
 * 既是2又是7的倍数输出 我是妈妈桑
 * 以上条件都不满足 输出 我是嫖客
 * * </p>
 *
 * @author zhangqing
 * @since 2021/3/2
 */


public class NumberGame {
    @Rule
    @Data
   public static class Duck{
        private int input;
        @Condition
        public boolean isDuck(){
            return input % 2 == 0;
        }
        @Action
        public void printDuck(){
            System.out.println(input + ":我是1只鸭");
        }
        @Priority
        public int getPriority(){
            return 1;
        }
    }
    @Data

    @Rule
    public static class Chicken{
        private int input;
        @Condition
        public boolean isChicken(){
            return input%7==0;
        }
        @Action
        public void printChicken(){
            System.out.println(input+ ":我是1只鸡");
        }
        @Priority
        public int getPriority(){
            return 2;
        }
    }

    public static class Mom extends CompositeRule {
        private Integer input;
        public Mom(Object ... rules)  {
            for (Object rule : rules) {

                addRule(rule);
            }
        }

        @Override
        public int getPriority() {
            return 0;
        }


        @Override
        public void execute() {

                System.out.println(this.input+":我是妈妈");

        }


}
    public static class Piao extends CompositeRule {
        private Integer input;
        public Piao(Object ... rules)  {
            for (Object rule : rules) {

                removeRule(rule);
            }
        }

        @Override
        public int getPriority() {
            return 4;
        }


        @Override
        public void execute() {

            System.out.println("我是嫖客");

        }


    }
    public static void main(String[] args) {

        RulesEngine rulesEngine = RulesEngineBuilder
                .aNewRulesEngine()
                .withSkipOnFirstAppliedRule(true)
                .withSilentMode(true)
                .build();
        Duck duck = new Duck();
        Chicken chicken = new Chicken();
        Mom mom = new Mom(duck,chicken);
        Piao piao = new Piao( mom);
        rulesEngine.registerRule(duck);
        rulesEngine.registerRule(chicken);
        rulesEngine.registerRule(mom);
        rulesEngine.registerRule(piao);
        for (int i = 0; i < 100; i++) {
            duck.setInput(i);
            chicken.setInput(i);
            rulesEngine.fireRules();

        }
    }
}
